function GameEngine() {
	if(GameEngine.instance) return GameEngine.instance; //单例模式
	this._table = null;
	this.snake = null;
	this.food = null;
	this.ROWS = 40;
	this.COLS = 40;
	GameEngine.instance = this;
}
GameEngine.prototype = {
	init: function() {
		//初始化页面元素
		this._table = $(".main");
		var frag = document.createDocumentFragment();
		for(var i = 0; i < this.ROWS; i++) {
			var _tr = document.createElement('tr');
			for(var j = 0; j < this.COLS; j++) {
				var _td = document.createElement('td');
				_tr.appendChild(_td);
			}
			frag.appendChild(_tr);
		}
		this._table.appendChild(frag);
		//随机蛇的位置
		this.snake = new Snake().init();
		//随机食物位置
		this.food = new Food().init();
		//开始游戏
		this.start();

	},
	start: function() {
		var _this = this;
		eventUtils.addEvent(document, 'keydown', function(e) {
			e = e || window.event;
			var dir = e.keycode || e.which;
			clearInterval(_this._interval);//函数节流
			_this._interval = setInterval(function() {
				switch(dir) {
					case 38:
						_this.snake.dir == 2 ? "" : _this.snake.dir = 0;
						break;
					case 39:
						_this.snake.div == 3 ? "" : _this.snake.dir = 1;
						break;
					case 40:
						_this.snake.dir == 0 ? "" : _this.snake.dir = 2;
						break;
					case 37:
						_this.snake.dir == 1 ? "" : _this.snake.dir = 3;
						break;
				}

			}, 200);

		});
	},
	addFood: function() {
		this.food = new Food().init();
	},
	stop: function() {
		clearInterval(this.snake._interval);
		alert("GAME OVER");
	}

}